package cn.edu.cque.mall.controller;

import cn.edu.cque.mall.entity.InfoResult;
import cn.edu.cque.mall.entity.User;
import cn.edu.cque.mall.service.UserService;
import cn.edu.cque.mall.utils.UUIDUtils;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName UserController
 * @Description
 * @Author chen
 * @Date 2020/10/11 0011 22:31
 */

@Controller
@RequestMapping("/user")
public class UserController{

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public InfoResult login(String username, String password, String verCode, HttpServletRequest request) {
        if (!CaptchaUtil.ver(verCode, request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            return InfoResult.ERROR("验证码不正确");
        }
        CaptchaUtil.clear(request);
        //校验用户名密码
        User user = userService.login(username, password);
        if (user != null) {
            //登录成功
            request.getSession().setAttribute("loginUser", user);
            return InfoResult.OK();
        } else {
            //登录失败
            return InfoResult.ERROR("用户名或密码错误");
        }
    }


    @RequestMapping("logout")
    @ResponseBody

    public InfoResult logout(HttpServletRequest request) {
        // 1 将用户信息从session中移除
        request.getSession().removeAttribute("loginUser");
        // 2 跳转页面
        return InfoResult.OK();
    }

    @RequestMapping("register")
    @ResponseBody
    public InfoResult register(HttpServletRequest request) {
        //2 对验证码进行校验
        String verCode = request.getParameter("verCode");
        if (!CaptchaUtil.ver(verCode, request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            return InfoResult.ERROR("验证码不正确");
        }
        CaptchaUtil.clear(request);
        //1 注册
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String name = request.getParameter("name");
        String telephone = request.getParameter("telephone");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        //将birthday转换成Date类型
        Date birthdays=null;
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        try {
            birthdays=format.parse(birthday);
            System.out.println(birthdays);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //随机获取一个uid值
        String uid = UUIDUtils.getId();
        String code = UUIDUtils.getId();
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhone(telephone);
        user.setName(name);
        user.setSex(sex);
        user.setBirthday(birthdays);
        user.setId(uid);
        if(username=="admin" && password=="admin"){
            user.setState(1);
        }else {
            user.setState(0);
        }
        user.setCode(code);
        userService.addUser(user);
        User newUser = userService.findByIdUser(uid);
        HttpSession session = request.getSession();
        session.setAttribute("newUser",newUser);
        //设置session存储时间
        session.setMaxInactiveInterval(30);
        return InfoResult.OK();

    }

}
